<template>
  <div>
    <el-header class="header">
      <span>用户管理</span>
      <div class="pull-right">
        <el-button size="small" type="primary" @click="isAddShow=true">添加用户</el-button>
      </div>
    </el-header>
    <el-table
      stripe
      border
      :data="userList"
      style="width: 100%">
      <el-table-column
        prop="id"
        label="ID"
        width="180">
      </el-table-column>
      <el-table-column
        prop="name"
        label="用户名"
        width="180">
      </el-table-column>
      <el-table-column
        prop="password"
        label="密码">
      </el-table-column>
      <el-table-column
        fixed="right"
        label="操作"
        width="100">
        <template slot-scope="scope">
          <el-button
            type="text"
            size="small"
            @click="deleteUser(scope.row.id)">删除</el-button>
        </template>
      </el-table-column>
    </el-table>
    <el-drawer
      :size="500"
      title="添加用户"
      :visible.sync="isAddShow">
      <Add v-if="isAddShow" @close="onAddClose()"></Add>
    </el-drawer>
  </div>
</template>

<script>
import Add from './components/Add.vue'

export default {
  components: {
    Add
  },
  data () {
    return {
      userList: [],
      isAddShow: false
    }
  },
  created() {
      this.getUser();
  },
  methods: {
    onAddClose () {
      this.isAddShow = false
      this.getUser()
    },
    getUser() {
      this.axios("/user/list").then((res) => {
          this.userList = res.data;
      });
    },
    async deleteUser (id) {
      try {
        await this.$confirm('确认删除吗?', '警告', {
          confirmButtonText: '确定',
          cancelButtonText: '取消',
          type: 'warning'
        })
        await this.axios.post("/user/delete", {id})
        this.getUser()
        this.$message({
          message: '删除成功',
          type: 'success'
        });
      } catch (error) {console.log('cancel')}
    }
  }
}
</script>
<style scoped>
.header {
  line-height: 60px;
}
</style>